let userList = [{
    id: '1',
    username: 'admin',
    password: 'admin',
    role: 'admin'
  },
  {
    id: '2',
    username: 'user',
    password: 'user',
    role: 'user'
  },
  {
    id: '3',
    username: 'zhangsan',
    password: 'zhangsan',
    role: 'user'
  },
  {
    id: '4',
    username: 'lisi',
    password: 'lisi',
    role: 'user'
  },
  {
    id: '5',
    username: 'wangwu',
    password: 'wangwu',
    role: 'user'
  }, {
    id: '6',
    username: 'zhaoliu',
    password: 'zhaoliu',
    role: 'user'
  }
]
const bodyParser = require('body-parser')
module.exports = app => {
  app.use(bodyParser.json());

  app.post('/login', (req, res) => {
    const {
      username,
      password
    } = req.body;

    const item = userList.find(v => v.username === username && v.password === password)

    if (item) {
      res.send({
        code: 200,
        message: '登录成功， 欢迎用户' + item.role,
        data: {
          role: item.role,
          username: item.username,
          id: item.id
        }
      })
    } else {
      res.send({
        code: 400,
        message: '账户密码错误， 请重新登录'
      })
    }
  })


  app.get('/user/list', (req, res) => {
    res.send({
      code: 200,
      data: userList,
      message: 'ok'
    })
  })

  app.delete('/user/list/:id', (req, res) => {
    const {
      id
    } = req.params;
    userList = userList.filter(v => v.id !== id)
    res.send({
      code: 200,
      message: 'ok'
    })
  })

  app.put('/user/list', (req, res) => {
    userList.push({
      id: Math.random().toString().slice(2),
      password: "123456",
      ...req.body
    })
    res.send({
      code: 200,
      message: 'ok'
    })
  })


  app.post('/user/list/:id', (req, res) => {
    const { id } = req.params;
    const item = userList.find(v => v.id === id);
    Object.assign(item, req.body)
    res.send({
      code: 200,
      message: 'ok'
    })
  })
}